import 'package:common_ui/common_ui.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';

class Avatar extends StatelessWidget {
  /// Create Avatar of all types i,e, square, circle, standard with different sizes.
  const Avatar(
      {Key? key,
      this.child,
      this.backgroundColor = Colors.grey,
      this.backgroundImage,
      this.foregroundColor,
      this.radius,
      this.alignment,
      this.minRadius,
      this.maxRadius,
      this.borderRadius,
      this.margin,
      this.shape = ShapeData.circle,
      this.size = 45})
      : assert(radius == null || (minRadius == null && maxRadius == null)),
        super(key: key);

  /// Typically a [Text] widget. If the [CircleAvatar] is to have an image, use [backgroundImage] instead.
  final Widget? child;
  final EdgeInsetsGeometry? margin;

  ///[child]所在的位置
  final AlignmentGeometry? alignment;

  /// use [Color] or [GFColors]. The color with which to fill the circle.
  final Color? backgroundColor;

  /// use [Color] or [GFColors]. The default text color for text in the circle.
  final Color? foregroundColor;

  /// The background image of the circle.
  final ImageProvider? backgroundImage;

  /// The size of the avatar, expressed as the radius (half the diameter).
  final double? radius;

  /// The minimum size of the avatar, expressed as the radius (half the diameter).
  final double? minRadius;

  /// The maximum size of the avatar, expressed as the radius (half the diameter).
  final double? maxRadius;

  final double size;

  /// shape of avatar [ShapeData] i.e, standard, circle, square
  final ShapeData shape;

  /// border radius to give extra radius for avatar square or standard shape
  /// Not applicable to circleshape
  final BorderRadius? borderRadius;

  // /// The default max if only the min is specified.
  // static const double _defaultMaxRadius = double.infinity;

  double get _minDiameter {
    if (radius == null && minRadius == null && maxRadius == null) {
      return size;
    } else {
      return 2.0 * (radius ?? minRadius ?? 0);
    }
  }

  double get _maxDiameter {
    if (radius == null && minRadius == null && maxRadius == null) {
      return 1.5 * size;
    } else {
      return 2.0 * (radius ?? maxRadius ?? 0);
    }
  }

  BoxShape get _avatarShape {
    if (shape == ShapeData.circle) {
      return BoxShape.circle;
    } else if (shape == ShapeData.square) {
      return BoxShape.rectangle;
    } else if (shape == ShapeData.standard) {
      return BoxShape.rectangle;
    } else {
      return BoxShape.rectangle;
    }
  }

  @override
  Widget build(BuildContext context) {
    final Color? backgroundColor = this.backgroundColor;
    final Color? foregroundColor = this.foregroundColor;
    assert(debugCheckHasMediaQuery(context));
    final ThemeData theme = Theme.of(context);
    TextStyle? textStyle = theme.primaryTextTheme.subtitle1?.copyWith(
      color: foregroundColor,
    );
    Color? effectiveBackgroundColor = backgroundColor;

    if (effectiveBackgroundColor == null && textStyle?.color != null) {
      switch (ThemeData.estimateBrightnessForColor(textStyle!.color!)) {
        case Brightness.dark:
          effectiveBackgroundColor = theme.primaryColorLight;
          break;
        case Brightness.light:
          effectiveBackgroundColor = theme.primaryColorDark;
          break;
      }
    } else if (foregroundColor == null && textStyle != null) {
      switch (ThemeData.estimateBrightnessForColor(backgroundColor!)) {
        case Brightness.dark:
          textStyle = textStyle.copyWith(color: theme.primaryColorLight);
          break;
        case Brightness.light:
          textStyle = textStyle.copyWith(color: theme.primaryColorDark);
          break;
      }
    }
    // final double minDiameter = _minDiameter;
    // final double maxDiameter = _maxDiameter;
    return AnimatedContainer(
      // constraints: BoxConstraints(
      //   minHeight: minDiameter,
      //   minWidth: minDiameter,
      //   maxWidth: maxDiameter,
      //   maxHeight: maxDiameter,
      // ),
      margin: margin,
      height: size,
      width: size,
      duration: kThemeChangeDuration,
      decoration: BoxDecoration(
        color: effectiveBackgroundColor,
        image: backgroundImage != null
            ? DecorationImage(
                image: backgroundImage!,
                fit: BoxFit.cover,
              )
            : null,
        shape: _avatarShape,
        borderRadius: shape == ShapeData.standard && borderRadius == null
            ? BorderRadius.circular(10)
            : borderRadius,
      ),
      alignment: alignment,
      child: child == null && textStyle != null ? null : child!,
    );
  }
}
